layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#newsList',
        url : 'searchAllArticle',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [10,15,20,25],
        id : "newsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50,},
            {field: 'articleId', title: '文章编号', width:60, align:"center",},
            {field: 'title', title: '文章标题', width:200,edit:"text"},
            {field: 'userName', title: '发布者', align:'center'},
            {field: 'acheck', title: '发布状态',  align:'center',templet:"#newsStatus"},
            {field: 'content', title: '具体内容', event: 'detail',align:'center',edit:"text"},
            {field: 'articleDate', title: '发表日期', align:'center'},
            {field: 'articlePicture', title: '发表图片', align:'center',templet:function (data) {
                if (data.articlePicture != null || data.articlePicture != ""){
                    return "<img src='../../"+ data.articlePicture+"'>";
                } else {
                    return "没有封面";
                }
                }},
            {field: 'simContent', title: '文章简介', align:'center',edit:"text"},
            {title: '操作', width:170, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    $(".refresh_btn").click(function(){
        tableIns.reload();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            articlesId = "";
        if(data.length > 0) {
            for (var i in data) {
                articlesId+= ","+ data[i].articleId;
            }
            layer.confirm('确定删除选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
                 $.get("deleteAllArticle",{
                     articlesId : articlesId  //将需要删除的articlesId作为参数传入
                 },function(result){
                     if (result.code == 100) {
                         tableIns.reload();
                         layer.close(index);
                         setTimeout(function(){
                             layer.msg("删除成功");
                         },500);
                     } else {
                         layer.msg("删除失败");
                     }
                 })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })

    table.on('edit(newsList)', function(obj){
        $.ajax({
            url : "updateArticle",
            type:"get",
            data : {
                field:obj.field,
                value:obj.value,
                articleId:obj.data.articleId
            },
            success : function (result) {
                if (result.code == 100){
                    layer.msg("更新成功")
                } else {
                    layer.msg("更新失败")
                }
            }
        });
    });

    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //通过
            var check = $.ajax({
                url : "checkArticle",
                type : "get",
                data : {articleId : data.articleId}
            });
            check.done(function (result) {
                if (result.code == 100){
                    tableIns.reload();
                    setTimeout(function(){
                        layer.msg("审核通过");
                    },500);

                } else {

                }
            })
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此文章？',{icon:3, title:'提示信息'},function(index){
                 $.get("deleteArticleByArticleId",{
                     articleId : data.articleId  //将需要删除的Id作为参数传入
                 },function(result){
                     if (result.code == 100 ) {
                         tableIns.reload();
                         layer.close(index);
                         setTimeout(function(){
                             layer.msg("删除成功");
                         },500);
                     } else {
                         setTimeout(function(){
                             layer.msg("删除失败");
                         },500);
                     }
                 })
            });
        } else if(layEvent === 'look'){ //预览文章的具体内容和封面
            var index = layui.layer.open({
                title : "预览文章",
                type : 2,
                content : "show?articleId="+data.articleId,
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            })

        }
    });

})